package com.ruoyi.car.service;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;

@Service
public class FormatService {
    @Autowired
    private ServerConfig serverConfig;

    public String replaceDomainName(String url) {
        if (StringUtils.isEmpty(url)) {
            return url;
        }
        String domain = serverConfig.getUrl();
        return replaceDomainName(url, domain);
    }

    /**
     * 外部渠道替换图片域名
     * 例如小程序发布体验版，用内网穿透域名访问，这时候必须将localhost域名替换，不然访问不到
     */
    public String replaceDomainName(String url, String domain) {
        try {
            URI uri = new URI(url);
            String path = uri.getPath();
            return domain + path;
        } catch (URISyntaxException e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
